<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>VueTestUtils笔记 | latte and cat</title>
    <meta name="description" content="study programs and record life">
    <meta name="generator" content="VuePress 1.4.0">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.e1c67194.css" as="style"><link rel="preload" href="/assets/js/app.ff77de4f.js" as="script"><link rel="preload" href="/assets/js/4.6c1b7be2.js" as="script"><link rel="preload" href="/assets/js/1.3ea07518.js" as="script"><link rel="preload" href="/assets/js/17.67155a52.js" as="script"><link rel="prefetch" href="/assets/js/10.18024e3f.js"><link rel="prefetch" href="/assets/js/11.a9ba7471.js"><link rel="prefetch" href="/assets/js/12.64cdb496.js"><link rel="prefetch" href="/assets/js/13.9fb4b156.js"><link rel="prefetch" href="/assets/js/14.c3753656.js"><link rel="prefetch" href="/assets/js/15.652b11fc.js"><link rel="prefetch" href="/assets/js/16.b24685ca.js"><link rel="prefetch" href="/assets/js/18.1d703305.js"><link rel="prefetch" href="/assets/js/19.0b6a6748.js"><link rel="prefetch" href="/assets/js/20.a576349c.js"><link rel="prefetch" href="/assets/js/21.4bd94152.js"><link rel="prefetch" href="/assets/js/22.1abe4122.js"><link rel="prefetch" href="/assets/js/23.d2352828.js"><link rel="prefetch" href="/assets/js/24.a69cf77c.js"><link rel="prefetch" href="/assets/js/25.40b2c71a.js"><link rel="prefetch" href="/assets/js/26.7da4b904.js"><link rel="prefetch" href="/assets/js/27.80a148eb.js"><link rel="prefetch" href="/assets/js/28.d8cf3072.js"><link rel="prefetch" href="/assets/js/5.60884555.js"><link rel="prefetch" href="/assets/js/6.e3bfbe92.js"><link rel="prefetch" href="/assets/js/7.f4dfd5c4.js"><link rel="prefetch" href="/assets/js/8.14ec5fab.js"><link rel="prefetch" href="/assets/js/9.e4d60889.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.af2c2885.js">
    <link rel="stylesheet" href="/assets/css/0.styles.e1c67194.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-319dd33c><div data-v-319dd33c><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-319dd33c data-v-319dd33c><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>latte and cat</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>橘子</span>
            
          <span data-v-1e2a0cc0>2019 - </span>
          2020
        </a></span></div></div> <div class="hide" data-v-319dd33c><header class="navbar" data-v-319dd33c><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/head.png" alt="latte and cat" class="logo"> <span class="site-name">latte and cat</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/摘记/" class="nav-link"><i class="iconfont undefined"></i>
  摘记
</a></li><li class="dropdown-item"><!----> <a href="/categories/随笔/" class="nav-link"><i class="iconfont undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/note/" class="nav-link router-link-active"><i class="iconfont reco-document"></i>
  Notes
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="http://photo.smallsunnyfox.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-blog"></i>
  PhotoAlbum
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/smallsunnyfox" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-319dd33c></div> <aside class="sidebar" data-v-319dd33c><div class="personal-info-wrapper" data-v-6c8ffc9c><!----> <h3 class="name" data-v-6c8ffc9c>
    橘子
  </h3> <div class="num" data-v-6c8ffc9c><div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>14</h3> <h6 data-v-6c8ffc9c>文章</h6></div> <div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>6</h3> <h6 data-v-6c8ffc9c>标签</h6></div></div> <hr data-v-6c8ffc9c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/笔记/" class="nav-link"><i class="iconfont undefined"></i>
  笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/摘记/" class="nav-link"><i class="iconfont undefined"></i>
  摘记
</a></li><li class="dropdown-item"><!----> <a href="/categories/随笔/" class="nav-link"><i class="iconfont undefined"></i>
  随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/note/" class="nav-link router-link-active"><i class="iconfont reco-document"></i>
  Notes
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><a href="http://photo.smallsunnyfox.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-blog"></i>
  PhotoAlbum
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/smallsunnyfox" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>HTML5</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/html5/HTML5的语义元素.html" class="sidebar-link">HTML5语义元素</a></li><li><a href="/note/html5/HTML5多媒体标签.html" class="sidebar-link">HTML5多媒体标签</a></li><li><a href="/note/html5/HTML5表单元素.html" class="sidebar-link">HTML5表单元素</a></li><li><a href="/note/html5/HTML5中的API.html" class="sidebar-link">HTML5中的API</a></li><li><a href="/note/html5/Canvas.html" class="sidebar-link">Canvas</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>CSS</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/css/css3新特性.html" class="sidebar-link">CSS3新特性</a></li><li><a href="/note/css/css3Flip.html" class="sidebar-link">CSS3实现翻转(Flip)效果</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>前端单元测试</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/note/fe-unit-test/chai.html" class="sidebar-link">chai.js笔记</a></li><li><a href="/note/fe-unit-test/mocha.html" class="sidebar-link">Mocha笔记</a></li><li><a href="/note/fe-unit-test/vueTestUtils.html" class="active sidebar-link">VueTestUtils笔记</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/note/fe-unit-test/vueTestUtils.html#起步demo" class="sidebar-link">起步demo</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/vueTestUtils.html#关于nexttick" class="sidebar-link">关于nextTick()</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/vueTestUtils.html#常用技巧" class="sidebar-link">常用技巧</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/vueTestUtils.html#测试键盘、鼠标等其它-dom-事件" class="sidebar-link">测试键盘、鼠标等其它 DOM 事件</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/vueTestUtils.html#配合vue-router-使用" class="sidebar-link">配合Vue Router 使用</a></li><li class="sidebar-sub-header"><a href="/note/fe-unit-test/vueTestUtils.html#在组件中测试vuex" class="sidebar-link">在组件中测试Vuex</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-1e2a0cc0 data-v-319dd33c><h3 class="title" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0>VueTestUtils笔记</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><input type="password" value="" data-v-1e2a0cc0> <span data-v-1e2a0cc0>Konck! Knock!</span> <button data-v-1e2a0cc0>OK</button></label> <div class="footer" style="display:none;" data-v-1e2a0cc0 data-v-1e2a0cc0><span data-v-1e2a0cc0><i class="iconfont reco-theme" data-v-1e2a0cc0></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-1e2a0cc0>vuePress-theme-reco</a></span> <span data-v-1e2a0cc0><i class="iconfont reco-copyright" data-v-1e2a0cc0></i> <a data-v-1e2a0cc0><span data-v-1e2a0cc0>橘子</span>
            
          <span data-v-1e2a0cc0>2019 - </span>
          2020
        </a></span></div></div> <div data-v-319dd33c><main class="page"><div class="page-title" style="display:none;"><h1>VueTestUtils笔记</h1> <hr> <div data-v-484a899e><i class="iconfont reco-account" data-v-484a899e><span data-v-484a899e>橘子</span></i> <!----> <i class="iconfont reco-eye" data-v-484a899e><span id="/note/fe-unit-test/vueTestUtils.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-484a899e><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-484a899e><span class="tag-item" data-v-484a899e>
      单元测试
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><p><a href="https://vue-test-utils.vuejs.org/zh/" target="_blank" rel="noopener noreferrer">官网<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p><a href="https://vue-test-utils.vuejs.org/zh/api/" target="_blank" rel="noopener noreferrer">API<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p><a href="https://vue-test-utils.vuejs.org/zh/api/wrapper/" target="_blank" rel="noopener noreferrer">Wrapper<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p><a href="https://vue-test-utils.vuejs.org/zh/api/wrapper-array/" target="_blank" rel="noopener noreferrer">WrapperArray<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p><a href="https://vue-test-utils.vuejs.org/zh/api/options.html" target="_blank" rel="noopener noreferrer">挂载选项<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p><a href="https://vue-test-utils.vuejs.org/zh/api/components/" target="_blank" rel="noopener noreferrer">组件<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <h2 id="起步demo"><a href="#起步demo" class="header-anchor">#</a> 起步demo</h2> <p>挂载 ==&gt; 测试 ==&gt; 模拟交互</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">/* eslint-disable no-unused-expressions */</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> mount <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@vue/test-utils'</span><span class="token comment">// 从测试实用工具集中导入 `mount()` 方法</span>
<span class="token keyword">import</span> Counter <span class="token keyword">from</span> <span class="token string">'@/components/counter.vue'</span><span class="token comment">// 导入要测试的组件</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> expect <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'chai'</span><span class="token comment">// 导入要使用的断言集</span>

describe<span class="token punctuation">.</span><span class="token function">only</span><span class="token punctuation">(</span><span class="token string">'Counter'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 现在挂载组件，你便得到了这个包裹器</span>
  <span class="token keyword">const</span> wrapper <span class="token operator">=</span> <span class="token function">mount</span><span class="token punctuation">(</span>Counter<span class="token punctuation">)</span>
  <span class="token comment">// 检查该组件渲染出来的 HTML 是否符合预期。</span>
  <span class="token function">it</span><span class="token punctuation">(</span><span class="token string">'renders the correct markup'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">expect</span><span class="token punctuation">(</span>wrapper<span class="token punctuation">.</span><span class="token function">html</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">contain</span><span class="token punctuation">(</span><span class="token string">'&lt;span class=&quot;count&quot;&gt;0&lt;/span&gt;'</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token comment">// 也便于检查已存在的元素</span>
  <span class="token function">it</span><span class="token punctuation">(</span><span class="token string">'has a button'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">expect</span><span class="token punctuation">(</span>wrapper<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">'button'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>to<span class="token punctuation">.</span>be<span class="token punctuation">.</span>ok
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token comment">// 模拟用户交互</span>
  <span class="token function">it</span><span class="token punctuation">(</span><span class="token string">'button click should increment the count'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">expect</span><span class="token punctuation">(</span>wrapper<span class="token punctuation">.</span>vm<span class="token punctuation">.</span>count<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toBe</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword">const</span> button <span class="token operator">=</span> wrapper<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'button'</span><span class="token punctuation">)</span><span class="token comment">// 定位按钮</span>
    button<span class="token punctuation">.</span><span class="token function">trigger</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">)</span><span class="token comment">// 模拟点击</span>
    <span class="token function">expect</span><span class="token punctuation">(</span>wrapper<span class="token punctuation">.</span>vm<span class="token punctuation">.</span>count<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toBe</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h2 id="关于nexttick"><a href="#关于nexttick" class="header-anchor">#</a> 关于nextTick()</h2> <blockquote><p>为了简化用法，Vue Test Utils 同步应用了所有的更新，所以你不需要在测试中使用 Vue.nextTick &gt; 来等待 DOM 更新。</p> <p>注意：当你需要为诸如异步回调或 Promise 解析等操作显性改进为事件循环的时候，nextTick 仍然是必要的。</p></blockquote> <p>如果你仍然需要在自己的测试文件中使用 <code>nextTick</code>，注意任何在其内部被抛出的错误可能都不会被测试运行器捕获，因为其内部使用了 Promise。</p> <p>关于这个问题有两个建议：</p> <ul><li>要么你可以在测试的一开始将 Vue 的全局错误处理器设置为 <code>done</code> 回调</li> <li>要么你可以在调用 <code>nextTick</code> 时不带参数让其作为一个 Promise 返回</li></ul> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 这不会被捕获</span>
<span class="token function">it</span><span class="token punctuation">(</span><span class="token string">'will time out'</span><span class="token punctuation">,</span> <span class="token parameter">done</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  Vue<span class="token punctuation">.</span><span class="token function">nextTick</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">expect</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toBe</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
    <span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 接下来的两项测试都会如预期工作</span>
<span class="token function">it</span><span class="token punctuation">(</span><span class="token string">'will catch the error using done'</span><span class="token punctuation">,</span> <span class="token parameter">done</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  Vue<span class="token punctuation">.</span>config<span class="token punctuation">.</span>errorHandler <span class="token operator">=</span> done
  Vue<span class="token punctuation">.</span><span class="token function">nextTick</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">expect</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toBe</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
    <span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token function">it</span><span class="token punctuation">(</span><span class="token string">'will catch the error using a promise'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> Vue<span class="token punctuation">.</span><span class="token function">nextTick</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">expect</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toBe</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="常用技巧"><a href="#常用技巧" class="header-anchor">#</a> 常用技巧</h2> <h3 id="测试什么"><a href="#测试什么" class="header-anchor">#</a> 测试什么</h3> <blockquote><p>对于UI组件的测试不要一味地追求行覆盖率，因为它会导致我们过分关注组件的内部实现细节，从而导致琐碎的测试。</p></blockquote> <p>取而代之的是，我们推荐把测试撰写为断言你的组件的公共接口，并在一个黑盒内部处理它。一个简单的测试用例将会断言一些输入 (用户的交互或 prop 的改变) 提供给某组件之后是否导致预期结果 (渲染结果或触发自定义事件)。</p> <p>比如，对于每次点击按钮都会将计数加一的 <code>Counter</code> 组件来说，其测试用例将会模拟点击并断言渲染结果会加 1。该测试并没有关注 <code>Counter</code> 如何递增数值，而只关注其输入和输出。</p> <p>该提议的好处在于，即便该组件的内部实现已经随时间发生了改变，只要你的组件的公共接口始终保持一致，测试就可以通过。</p> <h3 id="浅渲染"><a href="#浅渲染" class="header-anchor">#</a> 浅渲染</h3> <blockquote><p>浅渲染的背景</p> <p>在测试用例中，我们通常希望专注在一个孤立的单元中测试组件，避免对其子组件的行为进行间接的断言。</p> <p>额外的，对于包含许多子组件的组件来说，整个渲染树可能会非常大。重复渲染所有的子组件可能会让我们的测试变慢。</p></blockquote> <p>Vue Test Utils 允许你通过 <code>shallowMount</code> 方法只挂载一个组件而不渲染其子组件 (即保留它们的存根)</p> <h3 id="断言触发的事件"><a href="#断言触发的事件" class="header-anchor">#</a> 断言触发的事件</h3> <p>每个挂载的包裹器都会通过其背后的 Vue 实例自动记录所有被触发的事件。你可以用 <code>wrapper.emitted()</code> 方法取回这些事件记录。</p> <p>然后你可以基于这些数据来设置断言，你也可以调用 <code>wrapper.emittedByOrder()</code> 获取一个按触发先后排序的事件数组。</p> <h3 id="从子组件触发事件"><a href="#从子组件触发事件" class="header-anchor">#</a> 从子组件触发事件</h3> <p>你可以通过访问子组件实例来触发一个自定义事件</p> <h3 id="操作组件状态"><a href="#操作组件状态" class="header-anchor">#</a> 操作组件状态</h3> <p>你可以在包裹器上用 <code>setData</code> 或 <code>setProps</code> 方法直接操作组件状态</p> <h3 id="仿造-prop"><a href="#仿造-prop" class="header-anchor">#</a> 仿造 Prop</h3> <p>你可以使用 Vue 在内置 <code>propsData</code> 选项向组件传入 prop</p> <p>也可以用 <code>wrapper.setProps({})</code> 方法更新这些已经挂载的组件的 prop</p> <h3 id="应用全局的插件和混入"><a href="#应用全局的插件和混入" class="header-anchor">#</a> 应用全局的插件和混入</h3> <p>有些组件可能依赖一个全局插件或混入 (mixin) 的功能注入，比如 <code>vuex</code> 和 <code>vue-router</code>。</p> <p>如果你在为一个特定的应用撰写组件，你可以在你的测试入口处一次性设置相同的全局插件和混入。但是有些情况下，比如测试一个可能会跨越不同应用共享的普通的组件套件的时候，最好还是在一个更加隔离的设置中测试你的组件，不对全局的 <code>Vue</code> 构造函数注入任何东西。我们可以使用 <a href="https://vue-test-utils.vuejs.org/zh/api/createLocalVue.html" target="_blank" rel="noopener noreferrer"><code>createLocalVue</code><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 方法来存档它们</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> createLocalVue <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@vue/test-utils'</span>

<span class="token comment">// 创建一个扩展的 `Vue` 构造函数</span>
<span class="token keyword">const</span> localVue <span class="token operator">=</span> <span class="token function">createLocalVue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 正常安装插件</span>
localVue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>MyPlugin<span class="token punctuation">)</span>

<span class="token comment">// 在挂载选项中传入 `localVue`</span>
<span class="token function">mount</span><span class="token punctuation">(</span>Component<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  localVue
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="仿造注入"><a href="#仿造注入" class="header-anchor">#</a> 仿造注入</h3> <p>另一个注入 prop 的策略就是简单的仿造它们。你可以使用 <code>mocks</code> 选项</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> mount <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@vue/test-utils'</span>

<span class="token keyword">const</span> $route <span class="token operator">=</span> <span class="token punctuation">{</span>
  path<span class="token operator">:</span> <span class="token string">'/'</span><span class="token punctuation">,</span>
  hash<span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">,</span>
  params<span class="token operator">:</span> <span class="token punctuation">{</span> id<span class="token operator">:</span> <span class="token string">'123'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  query<span class="token operator">:</span> <span class="token punctuation">{</span> q<span class="token operator">:</span> <span class="token string">'hello'</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token function">mount</span><span class="token punctuation">(</span>Component<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  mocks<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token comment">// 在挂载组件之前</span>
    <span class="token comment">// 添加仿造的 `$route` 对象到 Vue 实例中</span>
    $route
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="存根组件"><a href="#存根组件" class="header-anchor">#</a> 存根组件</h3> <p>你可以使用 <code>stubs</code> 选项覆写全局或局部注册的组件</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> mount <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@vue/test-utils'</span>

<span class="token function">mount</span><span class="token punctuation">(</span>Component<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  <span class="token comment">// 将会把 globally-registered-component 解析为</span>
  <span class="token comment">// 空的存根</span>
  stubs<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'globally-registered-component'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="测试键盘、鼠标等其它-dom-事件"><a href="#测试键盘、鼠标等其它-dom-事件" class="header-anchor">#</a> 测试键盘、鼠标等其它 DOM 事件</h2> <h3 id="触发事件"><a href="#触发事件" class="header-anchor">#</a> 触发事件</h3> <p><code>Wrapper</code> 暴露了一个 <code>trigger</code> 方法。它可以用来触发 DOM 事件。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> wrapper <span class="token operator">=</span> <span class="token function">mount</span><span class="token punctuation">(</span>MyButton<span class="token punctuation">)</span>

wrapper<span class="token punctuation">.</span><span class="token function">trigger</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>find</code> 方法也会返回一个 <code>Wrapper</code>。假设 <code>MyComponent</code> 包含一个按钮，下面的代码会点击这个按钮。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> wrapper <span class="token operator">=</span> <span class="token function">mount</span><span class="token punctuation">(</span>MyComponent<span class="token punctuation">)</span>

wrapper<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token string">'button'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trigger</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="选项"><a href="#选项" class="header-anchor">#</a> 选项</h3> <p><code>trigger</code> 方法接受一个可选的 <code>options</code> 对象。这个 <code>options</code> 对象里的属性会被添加到事件中。</p> <p>注意其目标不能被添加到 <code>options</code> 对象中。</p> <h3 id="键盘限制"><a href="#键盘限制" class="header-anchor">#</a> 键盘限制</h3> <p>键盘可以用<code>wrapper.trigger('keydown.xxx')</code>来触发</p> <p>此种方式支持的按键名是有限的，这些按键名会被翻译成一个<code>keyCode</code></p> <table><thead><tr><th>key name</th> <th>key code</th></tr></thead> <tbody><tr><td>enter</td> <td>13</td></tr> <tr><td>esc</td> <td>27</td></tr> <tr><td>tab</td> <td>9</td></tr> <tr><td>space</td> <td>32</td></tr> <tr><td>delete</td> <td>46</td></tr> <tr><td>backspace</td> <td>8</td></tr> <tr><td>insert</td> <td>45</td></tr> <tr><td>up</td> <td>38</td></tr> <tr><td>down</td> <td>40</td></tr> <tr><td>left</td> <td>37</td></tr> <tr><td>right</td> <td>39</td></tr> <tr><td>end</td> <td>35</td></tr> <tr><td>home</td> <td>36</td></tr> <tr><td>pageup</td> <td>33</td></tr> <tr><td>pagedown</td> <td>34</td></tr></tbody></table> <p>其他按键的触发为下面这种形式</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>wrapper<span class="token punctuation">.</span><span class="token function">trigger</span><span class="token punctuation">(</span><span class="token string">'keydown'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
    key<span class="token operator">:</span> <span class="token string">'a'</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="配合vue-router-使用"><a href="#配合vue-router-使用" class="header-anchor">#</a> 配合Vue Router 使用</h2> <h3 id="在测试中安装vue-router"><a href="#在测试中安装vue-router" class="header-anchor">#</a> 在测试中安装Vue Router</h3> <p>在测试中，你应该杜绝在基本的 Vue 构造函数中安装 Vue Router。安装 Vue Router 之后 Vue 的原型上会增加 <code>$route</code> 和 <code>$router</code> 这两个只读属性。</p> <p>为了避免这样的事情发生，我们创建了一个 <code>localVue</code> 并对其安装 Vue Router。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> shallowMount<span class="token punctuation">,</span> createLocalVue <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@vue/test-utils'</span>
<span class="token keyword">import</span> VueRouter <span class="token keyword">from</span> <span class="token string">'vue-router'</span>

<span class="token keyword">const</span> localVue <span class="token operator">=</span> <span class="token function">createLocalVue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
localVue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>VueRouter<span class="token punctuation">)</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token function">shallowMount</span><span class="token punctuation">(</span>Component<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  localVue<span class="token punctuation">,</span>
  router
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><blockquote><p>**注意：**在一个 <code>localVue</code> 上安装 Vue Router 时也会将 <code>$route</code> 和 <code>$router</code>作为两个只读属性添加给该 <code>localVue</code>。这意味着如果你使用安装了 Vue Router 的 <code>localVue</code>，则不能在挂在一个组件时使用 <code>mocks</code> 选项来覆写 <code>$route</code> 和 <code>$router</code>。</p></blockquote> <h3 id="测试使用了-router-link-或-router-view-的组件"><a href="#测试使用了-router-link-或-router-view-的组件" class="header-anchor">#</a> 测试使用了 <code>router-link</code> 或 <code>router-view</code> 的组件</h3> <p>当你安装 Vue Router 的时候，<code>router-link</code> 和 <code>router-view</code> 组件就被注册了。这意味着我们无需再导入可以在应用的任意地方使用它们。</p> <p>当我们运行测试的时候，需要令 Vue Router 相关组件在我们挂载的组件中可用。有以下两种做法</p> <ul><li><p>使用存根</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> shallowMount <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@vue/test-utils'</span>

<span class="token function">shallowMount</span><span class="token punctuation">(</span>Component<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  stubs<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'router-link'</span><span class="token punctuation">,</span> <span class="token string">'router-view'</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p>为 localVue 安装 Vue Router</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> shallowMount<span class="token punctuation">,</span> createLocalVue <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@vue/test-utils'</span>
<span class="token keyword">import</span> VueRouter <span class="token keyword">from</span> <span class="token string">'vue-router'</span>

<span class="token keyword">const</span> localVue <span class="token operator">=</span> <span class="token function">createLocalVue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
localVue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>VueRouter<span class="token punctuation">)</span>

<span class="token function">shallowMount</span><span class="token punctuation">(</span>Component<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  localVue
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li></ul> <h3 id="伪造-route-和-router"><a href="#伪造-route-和-router" class="header-anchor">#</a> 伪造 $route 和 $router</h3> <p>有的时候你想要测试一个组件在配合 <code>$route</code> 和 <code>$router</code> 对象的参数时的行为。这时候你可以传递自定义假数据给 Vue 实例。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> shallowMount <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'@vue/test-utils'</span>

<span class="token keyword">const</span> $route <span class="token operator">=</span> <span class="token punctuation">{</span>
  path<span class="token operator">:</span> <span class="token string">'/some/path'</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> wrapper <span class="token operator">=</span> <span class="token function">shallowMount</span><span class="token punctuation">(</span>Component<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  mocks<span class="token operator">:</span> <span class="token punctuation">{</span>
    $route
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

wrapper<span class="token punctuation">.</span>vm<span class="token punctuation">.</span>$route<span class="token punctuation">.</span>path <span class="token comment">// /some/path</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><blockquote><p>安装 Vue Router 会在 Vue 的原型上添加 <code>$route</code> 和 <code>$router</code> 只读属性。</p> <p>这意味着在未来的任何测试中，伪造 <code>$route</code> 或 <code>$router</code> 都会失效。</p> <p>要想回避这个问题，就不要在运行测试的时候全局安装 Vue Router，而用上述的 <code>localVue</code> 用法。</p></blockquote> <h2 id="在组件中测试vuex"><a href="#在组件中测试vuex" class="header-anchor">#</a> 在组件中测试Vuex</h2> <h3 id="伪造action"><a href="#伪造action" class="header-anchor">#</a> 伪造Action</h3> <p>站在测试的角度，我们不关心这个 action 做了什么或者这个 store 是什么样子的。我们只需要知道这些 action 将会在适当的时机触发，以及它们触发时的预期值。</p> <p>为了完成这个测试，我们需要在浅渲染组件时给 Vue 传递一个伪造的 store。</p> <p>我们可以把 store 传递给一个 <a href="https://vue-test-utils.vuejs.org/zh/api/options.html#localvue" target="_blank" rel="noopener noreferrer"><code>localVue</code><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，而不是传递给基础的 Vue 构造函数。<code>localVue</code> 是一个独立作用域的 Vue 构造函数，我们可以对其进行改动而不会影响到全局的 Vue 构造函数。</p> <h3 id="伪造getter"><a href="#伪造getter" class="header-anchor">#</a> 伪造Getter</h3> <p>我们并不关注这些 getter 返回什么——只关注它们被正确的渲染。</p> <h3 id="伪造module"><a href="#伪造module" class="header-anchor">#</a> 伪造Module</h3> <p><a href="https://vuex.vuejs.org/zh/guide/modules.html" target="_blank" rel="noopener noreferrer">Module<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 对于将我们的 store 分隔成多个可管理的块来说非常有用。它们也暴露 getter。我们可以在测试中使用它们。</p> <h3 id="测试一个vuex-store"><a href="#测试一个vuex-store" class="header-anchor">#</a> 测试一个Vuex Store</h3> <p>这里有两个测试 Vuex store 的方式。第一个方式是分别单元化测试 getter、mutation 和 action。第二个方式是创建一个 store 并针对其进行测试。两种方式的比较如下。</p> <ul><li>分别测试 getter、mutation 和 action
Getter、mutation 和 action 全部是 js 函数，所以我们可以不通过 Vue Test Utils 和 Vuex 测试它们。
分别测试 getter、mutation 和 action 的好处是你的单元测试是非常详细的。当它们失败时，你完全知道你代码的问题是什么。当然另外一方面你需要伪造诸如 <code>commit</code> 和 <code>dispatch</code> 的 Vuex 函数。这会导致在一些情况下你伪造错了东西，导致单元测试通过，生产环境的代码却失败了。</li> <li>这样做的好处是我们不需要伪造任何 Vuex 函数。另一方面当一个测试失败时，排查问题的难度会增加。</li></ul></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----></main> <!----> <div style="display:none;" data-v-319dd33c data-v-319dd33c><div class="comments-wrapper" data-v-319dd33c><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div></div></div>
    <script src="/assets/js/app.ff77de4f.js" defer></script><script src="/assets/js/4.6c1b7be2.js" defer></script><script src="/assets/js/1.3ea07518.js" defer></script><script src="/assets/js/17.67155a52.js" defer></script>
  </body>
</html>
